package com.unicom.office.runtime;

import com.unicom.office.config.OfficeConfig;
import com.unicom.office.dataitem.DataItem;
import com.unicom.office.var.VarItem;

import java.util.*;

public abstract class OfficeRuntime {
    List<DataItem> list = new ArrayList<DataItem>();
    public void clear(){
        this.list.clear();
    }
    public void in(VarItem name, Object value){
        if(name == null) {
            throw new RuntimeException("不能为null");
        }
        list.add(name.createDataItem(value));
    }
    public void set(VarItem name, Object value){
        for (int i = list.size() - 1; i >= 0; i--) {
            DataItem dataItem = list.get(i);
            if(dataItem.getConfig() == name){
                dataItem.setValue(value);
                return;
            }
        }
    }
    public DataItem get(VarItem name){
        for (int i = list.size() - 1; i >= 0; i--) {
            DataItem dataItem = list.get(i);
            if(dataItem.getConfig() == name){
                return dataItem;
            }
        }
        throw new RuntimeException("变量不存在");
    }
//    public DataItem get(String name){
//        for (int i = list.size() - 1; i >= 0; i--) {
//            DataItem dataItem = list.get(i);
//            if(dataItem.getConfig().getName().equals(name)){
//                return dataItem;
//            }
//        }
//        throw new RuntimeException("变量不存在");
//    }
    public Object out(VarItem name){
        for (int i = list.size() - 1; i >= 0; i--) {
            DataItem dataItem = list.get(i);
            if(dataItem.getConfig() == name){
                list.remove(dataItem);
                return dataItem;
            }
        }
        return null;
    }
    public void in(Map<String, Object> map, OfficeConfig config){
        if(map == null){
            map = new HashMap<String, Object>();
        }

        Set<String> keySet = config.getVarItemMap().keySet();
        for (String key : keySet) {
            VarItem varItem = config.getVarItem(key);
            if(map.containsKey(key)) {
                in(varItem, map.get(key));
            }else {
                in(varItem, varItem.getDefaultValue());
            }
        }
    }
    public Map<String, Object> getData(){
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (DataItem dataItem : list) {
            map.put(dataItem.getConfig().getName(), dataItem.getValue());
        }
        return map;
    }


    public abstract void beforeParse();
    public abstract void afterParse();

    public abstract void beforeNodeParse();
    public abstract void afterNodeParse();

    public abstract void beforeRender();
    public abstract void afterRender();

    public abstract void beforeNodeRender();
    public abstract void afterNodeRender();

    public abstract void beforeBulid();
    public abstract void afterBulid();

    public abstract void beforeNodeBulid();
    public abstract void afterNodeBulid();

    public static final OfficeRuntime EMPTY = new OfficeRuntime() {
        public void beforeParse() {

        }

        public void afterParse() {

        }

        public void beforeNodeParse() {

        }

        public void afterNodeParse() {

        }

        public void beforeRender() {

        }

        public void afterRender() {

        }

        public void beforeNodeRender() {

        }

        public void afterNodeRender() {

        }

        public void beforeBulid() {

        }

        public void afterBulid() {

        }

        public void beforeNodeBulid() {

        }

        public void afterNodeBulid() {

        }
    };
}
